package java;

public class Question33 {
    public static void main(String[] args) {
        int[] nums = {4,5,6,7,0,1,2,3};
        System.out.println(search(nums,8));
    }
    public static int search(int[] nums, int target) {
        if (nums == null || nums.length < 1){
            return -1;
        }
        int left = 0;
        int right = nums.length - 1;

        while (left <= right){
            int center = (left + right)/2;
            if (nums[center] >= nums[left]){
                if (target < nums[center]){
                    if (target < nums[left]){
                        left = center + 1;
                    }else {
                        right = center - 1;
                    }
                }else if (target > nums[center]){
                    left = center + 1;
                }else {
                    return center;
                }
            }else {
                if (target < nums[center]){
                    right = center - 1;
                }else if (target > nums[center]){
                    if (target <= nums[right]){
                        left = center + 1;
                    }else {
                        right = center - 1;
                    }
                }else {
                    return center;
                }
            }
        }
        return -1;
    }
}
